<?php

namespace app\api\controller;

use app\common\controller\UnLogin;
use think\Db;

//use JPush\Push;

class Login extends UnLogin {

	//1=手机号码 2=用户名 3=邮箱
	private $login_type = [1,2];

	//接口防刷机制  1分钟10次
	public function initialize() {

		parent::initialize();
		//注册ip防刷限制
		$ip_limit = cache($this->ip.'/login');
		$time = time();
		if(!empty($ip_limit)){
			if($ip_limit['num'] >= 10){
				$this->retData(1, '接口调用频繁，请一分钟后再试');
			} else {
				$num = $ip_limit['num']+1;
				if ($num < 10) {
					$experi = $time-$ip_limit['time'];
					cache($this->ip.'/login',['num'=>$num,'time'=>$time],$experi);	
				} else {
					cache($this->ip.'/login',['num'=>$num,'time'=>$time],60);	
				}
				
			}
		} else {
			cache($this->ip.'/login',['num'=>1,'time'=>$time],60);
		}
	}

	/*
	登录
	*/
	public function login() {
		$account  = P('account');
		$account2 = P('account2');
		$password = P('password');
		$cap_code = P('cap_code'); //图形验证码
		$cap_key  = P('cap_key'); //图形验证码key
		if (empty($account)) {
			$this->retData(1, '缺少参数账户(account)');
		}
		if (empty($password)) {
			$this->retData(1, '缺少参数登录密码(password)');
		}
		// $captcha = model('CaptchaModel')->getCaptcha($cap_key);
		// if (!$captcha) {
		// 	$this->retData(1, '图形验证码错误');
		// }
		// if ($captcha['v'] != strtolower($cap_code)) {
		// 	$this->retData(1, '图形验证码错误');
		// }
		$match_mail = '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9]+\.[a-zA-Z]+(\.[a-zA-Z]+)?$/';
		if (!preg_match($match_mail, $account)) {
			if (in_array(1, $this->login_type)) {
				$user_info = model('UserModel')->infoByPhone($account);
			}
			if (in_array(3, $this->login_type)) {
				$user_info = model('UserModel')->infoByUsername($account);
			}
		} else {
			if (in_array(2, $this->login_type)) {
				//邮箱登录
				$user_info = model('UserModel')->infoByMail($account);
			}
			
		} 
        // var_dump($user_info);die();
		if (empty($user_info)) {
			$this->retData(1, '用户不存在');
		}
		if ($user_info['status'] == 0) {
			$this->retData(1, '账号已被冻结，禁止登录');
		}
		if (md5($password) != $user_info['password']) {
			$this->retData(1, '登录密码错误');
		}
		$data['uid'] = $user_info['id'];
		$data['username'] = $user_info['username'];
		$token = model('TokenModel')->setToken($data['uid']);
		// model('CaptchaModel')->delCaptcha($captcha['id']);
		if (!$token) {
			$this->retData(1, '登录失败');
		} else {
			$data['token'] = $token;
			$this->retData(0, '登录成功', $data);
		}
	}

	/*
	用户名 + 交易密码恢复
	 */
	public function forgetByPaypass(){
		$username = P('username');
		$password = P('password');
		$paypass  = P('paypass');
		if (empty($username)) {
			$this->retData(1, '缺少参数账户名(username)');
		}
		if (empty($password)) {
			$this->retData(1, '缺少参数登录密码(password)');
		}
		if (empty($paypass)) {
			$this->retData(1, '缺少参数交易密码(paypass)');
		}
		$user_info = model('UserModel')->infoByUsername($username);
		if (empty($user_info)) {
			$this->retData(1, '账户不存在');
		}
		$paypass_limit = cache($username.'forgetPassword/paypass');
		if ($paypass_limit) {
			$use_num       = C('paypass_use_num');
			if($paypass_limit['num'] >= $use_num){
				$this->retData(1, '交易密码错误上限，请一小时后再试');
			}
		}
		if ($paypass != $user_info['paypass']) {
			$time = time();
			if($paypass_limit){
				if($paypass_limit['num'] >= $use_num){
					$this->retData(1, '交易密码错误上限，请一小时后再试');
				} else {
					$num = $paypass_limit['num']+1;
					$left_num = $use_num - $num;
					if ($num < $use_num) {
						$experi = $time-$paypass_limit['time'];
						cache($username.'forgetPassword/paypass',['num'=>$num,'time'=>$time],$experi);	
						$this->retData(1, '交易密码错误，剩余'.$left_num.'次');
					} else {
						cache($username.'forgetPassword/paypass',['num'=>$use_num],3600);
						$this->retData(1, '交易密码错误上限，请一小时后再试');
					}
				}
			} else {
				cache($username.'forgetPassword/paypass',['num'=>1,'time'=>$time],3600);
				$this->retData(1, '交易密码错误，剩余2次');
			}
		}
		if (md5($password) == $user_info['password']) {
			$this->retData(1, '密码与原密码一致');
		}
		$res = model('UserModel')->updateUser($user_info['id'],['password'=>md5($password)]);
		if ($res) {
			$this->retData(0, '操作成功');
		} else {
			$this->retData(1, '操作失败');
		}
	}


	/*
	忘记密码--手机号码或邮箱找回
	*/
	public function forgetByCode() {
		$password = P('password');
		$code     = P('code');
		$account  = P('account');
		$type     = (int)P('type');
		if (empty($password)) {
			$this->retData(1, '缺少参数新密码(password)');
		}
		if (empty($code)) {
			$this->retData(1, '缺少参数验证码(code)');
		}
		if (empty($type)) {
			$this->retData(1, '缺少参数类型(type)');
		}
		if (empty($account)) {
			$this->retData(1, '缺少参数手机号或邮箱(account)');
		}
		if (!in_array($type, [1,2,3])) {
			$this->retData(1, '类型错误');
		}
		$username = P('username') ?: $account;
		if ($type == 1) {
			$phone = $account;
			$user_info = model('UserModel')->infoByPhone($phone);
			if (!$user_info) {
				$this->retData(1, '账号不存在');
			}
			if (C('sms_verify_button') == 1) {
				$veri_code = model('PhoneCodeModel')->getPhoneCode($phone, 1);
				if ($code != $veri_code) {
					$this->retData(1, '验证码错误或者已过期');
				}
			}
		} elseif ($type == 2) {
			$mail = $account;
			$user_info = model('UserModel')->infoByMail($mail);
			if (!$user_info) {
				$this->retData(1, '账号不存在');
			}
			if (C('mail_verify_button') == 1) {
				$veri_code = model('MailCodeModel')->getMailCode($mail, 1);
				if ($code != $veri_code) {
					$this->retData(1, '验证码错误或者已过期');
				}
			}
		} else {
			$this->retData(1, 'system error');
		}
		
		$match_password = '/^[0-9_a-zA-Z]{8,20}$/';
		if (!preg_match($match_password, $password)) {
			$this->retData(1, '登录密码8-20位,且只能为字母数字下划线组合');
		}
		$password = md5($password);
		if ($password == $user_info['password']) {
			$this->retData(1, '新密码和旧密码一致');
		}
		$data = [
			'password' => $password
		];
		$res = model('UserModel')->updateUser($user_info['id'], $data);
		if ($res) {
			$this->retData(0, '修改成功');
		} else {
			$this->retData(1, '修改失败');
		}
	}


	/*
	助记词恢复
	 */
	public function forgetByWords(){

	}
}